/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.retail;

import cart.ShoppingCart;
import cart.ShoppingCartItem;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Thai Ha Cong
 */
@Namespace("/")
public class ShoppingCartAction extends ActionSupport {

    private Map<Integer, Integer> newQuantity;
    private Map<Integer, Boolean> isRemove;
    private String button;

    public ShoppingCartAction() {
    }

    public Map<Integer, Integer> getNewQuantity() {
        return newQuantity;
    }

    public void setNewQuantity(Map<Integer, Integer> newQuantity) {
        this.newQuantity = newQuantity;
    }

    public Map<Integer, Boolean> getIsRemove() {
        return isRemove;
    }

    public void setIsRemove(Map<Integer, Boolean> isRemove) {
        this.isRemove = isRemove;
    }

    public String getButton() {
        return button;
    }

    public void setButton(String button) {
        this.button = button;
    }

    @Override
    public String execute() throws Exception {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Action(value = "viewShoppingCart", results = {
        @Result(name = "success", location = "/shoppingCart.jsp")
    })
    public String view() {
        return SUCCESS;
    }

    @Action(value = "updateShoppingCart", results = {
        @Result(name = "success", location = "/redirect/rdrViewShoppingCart.jsp"),
        @Result(name = "error", location = "/shoppingCart.jsp"),
        @Result(name = "input", location = "/shoppingCart.jsp")
    })
    public String update() {
        Map session = ActionContext.getContext().getSession();
        ShoppingCart sc = (ShoppingCart) session.get("shoppingCart");
        List<ShoppingCartItem> cartItems = sc.getShoppingCartItemList();

        if (button.equals("Update")) {
//            System.out.println("Update");
//            System.out.println(newQuantity.size());

            for (int i = 0; i < newQuantity.size(); i++) {
                Integer nq = newQuantity.get(i);
                if (nq == null || nq < 0 || nq > 5) {
                    addActionError("Quantity is Limited by 5 per Customer.");
                    return ERROR;
                }
            }

            for (int i = 0; i < newQuantity.size(); i++) {
                cartItems.get(i).setQuantity(newQuantity.get(i));
            }

            for (int i = newQuantity.size() - 1; i >= 0; i--) {
                if (newQuantity.get(i) == 0) {
                    cartItems.remove(i);
                }
            }
        } else {
            List<Integer> rmv = new ArrayList<Integer>();

            if (isRemove != null) {
                if (isRemove.isEmpty() == false) {
                    for (Integer key : isRemove.keySet()) {
                        rmv.add(key);
                    }

                    for (int i = rmv.size() - 1; i >= 0; i--) {
                        cartItems.remove((int) rmv.get(i));
                    }
                }
            }
        }

        return SUCCESS;
    }
}